﻿using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using System.Xml;
using System.Xml.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKMenuAssist.Controls;
using YJKMenuAssist.Utility;

namespace YJKMenuAssist
{
	public partial class MenuManageForm : Window
	{
		public MenuManageForm(CommandTableInfos commandTableInfos, ExternalCommandData commandData)
		{
			this.InitializeComponent();
			this.m_CommandTableInfos = commandTableInfos;
			this.m_CommandData = commandData;
			this.Init();
		}

		private void Init()
		{
			if (this.m_CommandData == null)
			{
				return;
			}
			this.m_Doc = this.m_CommandData.Application.ActiveUIDocument.Document;
			this.CreateTemporaryFile();
			this.m_XmlManager = new XmlManager(this.m_StrFileName);
			this.m_XmlManager.ImportOpeningByXml(out this.m_KeyCommandTableInfos, "Original");
            //this.autoDefMenuTab.IniData(this.m_CommandTableInfos);
            //this.autoKeyBoardTab.IniData(this.m_KeyCommandTableInfos, this.m_CommandData, ShortcutsFile.GetKeyboardShortcutsPath(this.m_CommandData.Application.ActiveUIDocument.Document));
            //this.autoDefMenuTab.rightList.TabKey = this.tabKey;
			base.Icon = Common.ToImageSource(YArchitech.LIB.Resources.YArchitech);
		}

		private void CreateTemporaryFile()
		{
			string keyboardShortcutsPath = ShortcutsFile.GetKeyboardShortcutsPath(this.m_Doc);
			string text = Path.Combine(Product.UserDataLocation, "KeyboardShortcuts.xml");
			if (!File.Exists(text))
			{
				File.Copy(keyboardShortcutsPath, text);
			}
		}

		private void SetImage(string url, Image image)
		{
			Uri uriSource = new Uri(url, UriKind.Relative);
			image.Source = new BitmapImage(uriSource);
		}

		private void menuManageForm_Closing(object sender, CancelEventArgs e)
		{
            //this.autoDefMenuTab.rightList.IsMainWindowClose = true;
		}

		private void BtnDefaultVal_Click(object sender, RoutedEventArgs e)
		{
			string text = Path.Combine(Product.DataLocation, "CustomMenu.xml");
			string strKeyFile = Path.Combine(Product.UserDataLocation, "KeyboardShortcuts.xml");
			XmlManager xmlManager = new XmlManager(text);
			xmlManager.ImportOpeningByXml(out this.m_KeyCommandTableInfos, "Original");
            //this.autoKeyBoardTab.IniData(this.m_KeyCommandTableInfos, this.m_CommandData, strKeyFile);
			xmlManager.ImportOpeningByXml(out this.m_CommandTableInfos, "Original");
            //this.autoDefMenuTab.IniData(this.m_CommandTableInfos);
            //this.autoDefMenuTab.rightList.CommandTableInfos = this.m_CommandTableInfos;
            //this.autoDefMenuTab.rightList.treeView.Items.Clear();
            //this.autoDefMenuTab.rightList.IniTreeData(text);
            //this.autoDefMenuTab.centerList.treeView.Items.Clear();
		}

		private void BtnApplication_Click(object sender, RoutedEventArgs e)
		{
            //this.m_XmlManager.ExportMenuXml(this.autoDefMenuTab.rightList.treeView, this.m_CommandTableInfos);
			this.SetKeyBoard();
		}

		private void BtnOK_Click(object sender, RoutedEventArgs e)
		{
            //this.m_XmlManager.ExportMenuXml(this.autoDefMenuTab.rightList.treeView, this.m_CommandTableInfos);
			this.SetKeyBoard();
			base.DialogResult = new bool?(true);
		}

		private void SetKeyBoard()
		{
			string keyboardShortcutsPath = ShortcutsFile.GetKeyboardShortcutsPath(this.m_Doc);
			XElement xelement = XElement.Load(keyboardShortcutsPath);
			for (int i = 0; i < this.m_KeyCommandTableInfos.TableInofList.Count; i++)
			{
				TableInfos tableInfos = this.m_KeyCommandTableInfos.TableInofList[i];
				for (int j = 0; j < tableInfos.CommandInfoList.Count; j++)
				{
					if (tableInfos.CommandInfoList[j].LstCommandInfos.Count == 0)
					{
						ShortcutsFile.SetOneKeyboardShortcuts(this.m_Doc, tableInfos.CommandInfoList[j], tableInfos.TableName, xelement);
					}
					else
					{
						foreach (CommandInfos commandInfos in tableInfos.CommandInfoList[j].LstCommandInfos)
						{
							ShortcutsFile.SetOneKeyboardShortcuts(this.m_Doc, commandInfos, tableInfos.TableName, xelement);
						}
					}
				}
			}
			xelement.Save(keyboardShortcutsPath);
		}

		private void BtnCancel_Click(object sender, RoutedEventArgs e)
		{
			base.DialogResult = new bool?(false);
		}

		public static void SetFormParentHandle(Window form)
		{
			new WindowInteropHelper(form).Owner = Process.GetCurrentProcess().MainWindowHandle;
		}

		private void BtnExport_Click(object sender, RoutedEventArgs e)
		{
			ExportForm exportForm = new ExportForm();
			MenuManageForm.SetFormParentHandle(exportForm);
			exportForm.ShowDialog();
			if (exportForm.DialogResult == true)
			{
				try
				{
					if (!(exportForm.cbKey.IsChecked == false) || !(exportForm.cbMenu.IsChecked == false))
					{
						string text = Path.Combine(Product.UserDataLocation, "CustomMenu.xml");
						string keyboardShortcutsPath = ShortcutsFile.GetKeyboardShortcutsPath(this.m_Doc);
						if (exportForm.cbKey.IsChecked == true && exportForm.cbMenu.IsChecked == true)
						{
							XmlManager.MergeXml(keyboardShortcutsPath, text).Save(exportForm.textBox.Text + "\\用户配置.xml");
						}
						if (exportForm.cbKey.IsChecked == false && exportForm.cbMenu.IsChecked == true)
						{
							MenuManageForm.CopyFile(text, exportForm.textBox.Text + "\\菜单配置.xml");
						}
						if (exportForm.cbKey.IsChecked == true && exportForm.cbMenu.IsChecked == false)
						{
							MenuManageForm.CopyFile(keyboardShortcutsPath, exportForm.textBox.Text + "\\快捷键配置.xml");
						}
						YJKMessageBox.Show("成功导出！");
					}
				}
				catch
				{
				}
			}
		}

		private static void CopyFile(string strFileName, string strDesFileName)
		{
			File.Copy(strFileName, strDesFileName, true);
		}

		private void BtnImport_Click(object sender, RoutedEventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "xml file |*.xml";
			if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(openFileDialog.FileName))
			{
				try
				{
					List<string> list = this.ImportXml(openFileDialog.FileName);
					string text = Path.Combine(Product.UserDataLocation, "CustomMenuImport.xml");
					string text2 = Path.Combine(Product.UserDataLocation, "KeyboardShortcutsImport.xml");
					if (list.Contains("Tabs") && list.Contains("Shortcuts"))
					{
						XmlManager.SplitXml(openFileDialog.FileName, text2).Save(text);
						if (!this.ImportKey(text2, text))
						{
							return;
						}
						if (!this.ImportMenu(text))
						{
							return;
						}
					}
					else if (!list.Contains("Tabs") && list.Contains("Shortcuts"))
					{
						MenuManageForm.CopyFile(openFileDialog.FileName, text2);
						if (!this.ImportKey(text2, text))
						{
							return;
						}
					}
					else
					{
						if (!list.Contains("Tabs") || list.Contains("Shortcuts"))
						{
							YJKMessageBox.Show("导入文件格式不正确！");
							return;
						}
						MenuManageForm.CopyFile(openFileDialog.FileName, text);
						if (!this.ImportMenu(text))
						{
							return;
						}
					}
					YJKMessageBox.Show("成功导入！");
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show("导入文件格式不正确！" + ex.Message);
				}
			}
		}

		private bool ImportKey(string strKeyFileName, string strMenuFileName)
		{
			if (!new XmlManager(strMenuFileName).ImportOpeningByXml(out this.m_KeyCommandTableInfos, "Original"))
			{
				return false;
			}
            //this.autoKeyBoardTab.IniData(this.m_KeyCommandTableInfos, this.m_CommandData, strKeyFileName);
			return true;
		}

		private bool ImportMenu(string strMenuFileName)
		{
			if (!new XmlManager(strMenuFileName).ImportOpeningByXml(out this.m_CommandTableInfos, "Original"))
			{
				return false;
			}
            //this.autoDefMenuTab.IniData(this.m_CommandTableInfos);
            //this.autoDefMenuTab.rightList.treeView.Items.Clear();
            //this.autoDefMenuTab.rightList.IniTreeData(strMenuFileName);
            //this.autoDefMenuTab.centerList.treeView.Items.Clear();
			return true;
		}

		private List<string> ImportXml(string strFileName)
		{
			List<string> list = new List<string>();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(strFileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("MYCONFIG");
			if (xmlNode != null)
			{
				IEnumerator enumerator = xmlNode.ChildNodes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						XmlNode xmlNode2 = (XmlNode)obj;
						if (!list.Contains(xmlNode2.Name))
						{
							list.Add(xmlNode2.Name);
						}
					}
					return list;
				}
			}
			xmlNode = xmlDocument.SelectSingleNode("Shortcuts");
			if (xmlNode != null)
			{
				list.Add("Shortcuts");
			}
			return list;
		}

		private void menuManageForm_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if (e.Key == Key.Escape)
			{
				base.DialogResult = new bool?(false);
			}
		}

		private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
		}

		private CommandTableInfos m_CommandTableInfos;

		private CommandTableInfos m_KeyCommandTableInfos;

		private ExternalCommandData m_CommandData;

		private Document m_Doc;

		private string m_StrFileName = Path.Combine(Product.UserDataLocation, "CustomMenu.xml");

		private XmlManager m_XmlManager;
	}
}
